package br.cns.models;

import br.cns.util.RandomUtils;

public class ErdosRenyiM extends GenerativeProcedure {
	private int m;

	public ErdosRenyiM(int m) {
		this.m = m;
	}

	public ErdosRenyiM(double density, double numNodes) {
		this.m = (int)Math.round((density * (numNodes - 1) * numNodes)/2);
	}

	@Override
	public int[][] transform(int[][] matrix) {
		int[][] newMatrix = new int[matrix.length][matrix.length];

		int mc = 0;
		while (mc < m){
			int node1 = RandomUtils.getInstance().nextInt(matrix.length-1);
			int node2 = RandomUtils.getInstance().nextInt(matrix.length-1);
			while (node2 == node1 || newMatrix[node1][node2] == 1){
				node1 = RandomUtils.getInstance().nextInt(matrix.length-1);
				node2 = RandomUtils.getInstance().nextInt(matrix.length-1);
			}
			newMatrix[node1][node2] = 1;
			newMatrix[node2][node1] = 1;
			mc++;
		}
		return newMatrix;
	}

	@Override
	public String name() {
		return TModel.ERDOS_RENYI_N_M.toString();
	}

}
